package com.xxf.aynu.parking;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;

public class MainActivity extends AppCompatActivity {
    private LocationClient locationClient;
    public MyLocationListenner myListener = new MyLocationListenner();
    private MapView mapView;
    private BaiduMap baiduMap;
    boolean isFirstLoc = true;

    RadioGroup group;
    RadioButton qiang;
    RadioButton ting;
    RadioButton shouc;
    RadioButton my;
    ImageView img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        //获取百度地图控件
        mapView = (MapView) findViewById(R.id.bm);
        //获取百度地图对象
        baiduMap = mapView.getMap();
        // 开启定位图层
        baiduMap.setMyLocationEnabled(true);
        /**
         * 定位初始化
         */
        //声明定位SDK核心类
        locationClient = new LocationClient(this);
        //注册监听
        locationClient.registerLocationListener(myListener);
        //定位配置信息
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);//定位请求时间间隔
        locationClient.setLocOption(option);
        //开启定位
        locationClient.start();
        group=(RadioGroup)findViewById(R.id.tab_radioGroup);
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch(checkedId)
                {

                    case R.id.rb_world:
                        Intent intent=new Intent();
                        intent.setClass(getApplicationContext(), activity_youhui.class);
                        startActivity(intent);
                        onDestroy();
                        break;
                    case R.id.rb_home:
                        Intent intent1=new Intent();
                        intent1.setClass(getApplicationContext(), activity_showmap.class);
                        startActivity(intent1);
                        onDestroy();
                        break;
                    case R.id.rb_mine:
                        Intent intent2=new Intent();
                        intent2.setClass(getApplicationContext(), activity_mine_unlog.class);
                        //item.setIntent(intent);
                        startActivity(intent2);
                        onDestroy();
                        break;
                }

            }
        });

    }
    public class MyLocationListenner implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location)
        {
            if(location==null||mapView==null)
            {return;}
            MyLocationData locaData = new MyLocationData.Builder().accuracy(location.getRadius())
                    .direction(100).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            baiduMap.setMyLocationData(locaData);
            if (isFirstLoc) {
                isFirstLoc = false;
                LatLng ll = new LatLng(location.getLatitude(),
                        location.getLongitude());
                MapStatus.Builder builder = new MapStatus.Builder();
                builder.target(ll).zoom(18.0f);
                baiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
            }

        }
        public  void onReceivePoi(BDLocation poiLocation){

        }
        public void onConnectHotSpotMessage(String s, int i){

        }
    }
    @Override
    protected void onPause() {
        mapView.onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        mapView.onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        // 销毁搜索模块
        // 关闭定位图层
        baiduMap.setMyLocationEnabled(false);
        mapView.onDestroy();
        mapView = null;
        super.onDestroy();

    }

}
